package com.bcmusic.user.controller;

import com.bcmusic.file.feign.FileFeign;
import com.bcmusic.user.config.TokenDecode;
import com.bcmusic.user.dao.UserMapper;
import com.bcmusic.user.pojo.User;
import com.bcmusic.user.pojo.UserIdentification;
import com.bcmusic.user.service.UserIdentificationService;
import com.bcmusic.user.service.UserService;
import com.github.pagehelper.PageInfo;
import entity.IdWorker;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/userIdentification")
@CrossOrigin
public class UserIdentificationController {

    @Autowired
    private UserIdentificationService userIdentificationService;

    @Autowired
    private UserService userService;

    @Autowired
    private FileFeign fileFeign;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TokenDecode tokenDecode;

    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) UserIdentification userIdentification, @PathVariable int page, @PathVariable int size) {
        //调用UserIdentificationService实现分页条件查询UserIdentification
        PageInfo<UserIdentification> pageInfo = userIdentificationService.findPage(userIdentification, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * UserIdentification分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用UserIdentificationService实现分页查询UserIdentification
        PageInfo<UserIdentification> pageInfo = userIdentificationService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param userIdentification
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<UserIdentification>> findList(@RequestBody(required = false) UserIdentification userIdentification) {
        //调用UserIdentificationService实现条件查询UserIdentification
        List<UserIdentification> list = userIdentificationService.findList(userIdentification);
        return new Result<List<UserIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        //调用UserIdentificationService实现根据主键删除
        userIdentificationService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改UserIdentification数据
     * @param userIdentification
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody UserIdentification userIdentification, @PathVariable Long id) {
        //设置主键值
        userIdentification.setIdentificationId(id);
        //调用UserIdentificationService实现修改UserIdentification
        userIdentificationService.update(userIdentification);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增UserIdentification数据
     * @param userIdentification
     * @return
     */
    @PostMapping
    public Result add(@RequestBody UserIdentification userIdentification) {
        //调用UserIdentificationService实现添加UserIdentification
        userIdentificationService.add(userIdentification);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询UserIdentification数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UserIdentification> findById(@PathVariable Long id) {
        //调用UserIdentificationService实现根据主键查询UserIdentification
        UserIdentification userIdentification = userIdentificationService.findById(id);
        return new Result<UserIdentification>(true, StatusCode.OK, "查询成功", userIdentification);
    }

    /***
     * 查询UserIdentification全部数据
     * @return
     */
    @GetMapping
    public Result<List<UserIdentification>> findAll() {
        //调用UserIdentificationService实现查询所有UserIdentification
        List<UserIdentification> list = userIdentificationService.findAll();
        return new Result<List<UserIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 获取该用户信息
     *
     * @return
     */
    @GetMapping("accountInfo")
    public Result<UserIdentification> getAccountInfo() {
        String userNum = "202000000006";
        UserIdentification userIdentification = new UserIdentification();
        userIdentification.setUserOrdinaryNumber(userNum);
        userIdentification = userIdentificationService.findList(userIdentification).get(0);
        return new Result<UserIdentification>(true, StatusCode.OK, "查询成功", userIdentification);
    }

//    /**
//     * 更改用户头像(普通用户)
//     * @return
//     */
//    @PostMapping("updatePhoto")
//    public Result<UserIdentification> updatePhoto(MultipartFile file)
//    {
//        String userNum="202000000006";
//        String upload = fileFeign.upload(file);
//        UserIdentification userIdentification=new UserIdentification();
//        userIdentification.setUserOrdinaryNumber(userNum);
//        userIdentification = userIdentificationService.findList(userIdentification).get(0);
//        userIdentification.setProfilePhoto(upload);
//        userIdentificationService.update(userIdentification);
//
//        return new Result<UserIdentification>(true,StatusCode.OK,"更改头像成功",userIdentification);
//    }

    /**
     * 未认证用户进行身份认证
     * 或者已认证普通用户进行身份修改
     * 普通用户类型的
     * 新加数据
     */
    @Transactional
    @PostMapping("authentication")
    public Result<UserIdentification> authentication(@RequestBody UserIdentification userIdentification) {
        String userNum = tokenDecode.getUserInfo().get("id");
        User user = userService.findUserByNum(userNum);
        String photo=null;
        //如果认证过就将上一条信息作废
        if (user.getUserIdentiId() != 0) {
            UserIdentification old = new UserIdentification();
            old.setIdentificationId(user.getUserIdentiId());
            old.setValidStatus(-2);
            old = userIdentificationService.updateSelective(old);
            photo=old.getProfilePhoto();
        }
        userIdentification.setUserOrdinaryNumber(userNum);
        userIdentification.setSubmitTime(new Date());
        userIdentification.setValidStatus(0);
        userIdentification.setSubmitterNumber(userNum);
        //保留头像
        userIdentification.setProfilePhoto(photo);
        userIdentification = userIdentificationService.add(userIdentification);
        user.setUserIdentiId(userIdentification.getIdentificationId());
        user.setUserType(5);
        userService.updateSelective(user);
        return new Result<>(true, StatusCode.OK, "认证成功", userIdentification);
    }
}
